﻿Imports System.IO

Public Class Lab4_ask1

    Private source As String
    Private destination As String
    Private dfile As String
    Private file As String


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        OpenFileDialog1.Filter = "txt files |*.txt"
        OpenFileDialog1.ShowDialog()
        source = OpenFileDialog1.FileName
        TextBox1.Text = source
        Dim fi As New FileInfo(OpenFileDialog1.FileName.ToString)
        Label5.Text = fi.Extension
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        FolderBrowserDialog1.ShowDialog()
        destination = FolderBrowserDialog1.SelectedPath
        Label4.Text = destination
    End Sub

    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
        If TextBox2.Text <> String.Empty Then
            Button1.Enabled = True
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        dfile = TextBox2.Text
        CopyMe()
        WriteMe()
    End Sub
    Private Sub open(ByVal source As String, ByVal length As Long)
        If length > 100000 Then ' erwthma 4
            Throw New Over100KBException(source)
        End If
    End Sub
    Private Sub CopyMe()
        Dim strLine As String
        Dim objStreamReader As StreamReader
        Try  'erwthma 2,3
            objStreamReader = New StreamReader(source)
            Dim fi As New FileInfo(source)
            Dim length As Long = fi.Length 'bytes
            open(source, length)
            If length > 100000 Then
                Stop
            End If
            strLine = objStreamReader.ReadLine
            Do While Not strLine Is Nothing
                file += strLine & vbCrLf
                strLine = objStreamReader.ReadLine
            Loop

        Catch e As FileNotFoundException
            MessageBox.Show("FileNotFoundException exception Found")
        Catch ex1 As UnauthorizedAccessException
            MessageBox.Show("UnauthorizedAccessException exception Found")
        Catch ex2 As DirectoryNotFoundException
            MessageBox.Show("DirectoryNotFoundException exception Found")
        Catch ex3 As EndOfStreamException
            MessageBox.Show("EndOfStreamException exception Found")
        Catch ex4 As FileLoadException
            MessageBox.Show("FileLoadException exception Found")
        Catch ex5 As PathTooLongException
            MessageBox.Show("PathTooLongException exception Found")
        Catch ex6 As Over100KBException
            MessageBox.Show("Over100KBException exception Found")
        Finally
            objStreamReader.Close()

        End Try
    End Sub

    Private Sub WriteMe()
        Dim objStreamWriter As StreamWriter
        Try  'erwthma 1
            objStreamWriter = New StreamWriter(destination + "\" + dfile + Label5.Text, True)
            objStreamWriter.Write(file)
            MessageBox.Show("Copy Done")
        Catch
        Finally
            objStreamWriter.Close()
        End Try

    End Sub

End Class